<?php
/**
 * 数据导出记录表创建脚本
 */

require_once __DIR__ . '/vendor/autoload.php';

use think\facade\Db;

try {
    // 读取数据库配置
    $config = include __DIR__ . '/config/database.php';
    $mysqlConfig = $config['connections']['mysql'];
    
    // 创建数据库连接
    $dsn = sprintf(
        'mysql:host=%s;port=%d;dbname=%s;charset=%s',
        $mysqlConfig['host'],
        $mysqlConfig['port'],
        $mysqlConfig['database'],
        $mysqlConfig['charset']
    );
    
    $pdo = new PDO($dsn, $mysqlConfig['username'], $mysqlConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // SQL语句
    $sql = "CREATE TABLE IF NOT EXISTS `gf_data_exports` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '导出记录ID',
  `user_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '用户ID',
  `app_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '应用ID',
  `export_type` varchar(50) NOT NULL DEFAULT '' COMMENT '导出类型:basic,statistics,installs,events,comprehensive',
  `file_path` varchar(500) NOT NULL DEFAULT '' COMMENT '文件路径',
  `file_size` varchar(50) NOT NULL DEFAULT '' COMMENT '文件大小',
  `record_count` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '记录数量',
  `date_range_start` date DEFAULT NULL COMMENT '数据开始日期',
  `date_range_end` date DEFAULT NULL COMMENT '数据结束日期',
  `format` varchar(20) NOT NULL DEFAULT 'excel' COMMENT '文件格式:excel,csv,json',
  `advanced_options` text COMMENT '高级选项JSON',
  `status` varchar(20) NOT NULL DEFAULT 'pending' COMMENT '状态:pending,processing,completed,failed',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_app_id` (`app_id`),
  KEY `idx_status` (`status`),
  KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='数据导出记录表';";
    
    $pdo->exec($sql);
    
    echo "数据导出记录表创建成功！\n";
    
} catch (Exception $e) {
    echo "创建表失败: " . $e->getMessage() . "\n";
    echo "错误详情: " . $e->getTraceAsString() . "\n";
}